﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class RouteInfoController : BaseController
    {
        private readonly IMapper _mapper;
        private readonly RouteInfoApp _routeInfoApp;
        private readonly RouteDetailInfoApp _routeDetailInfoApp;
        private readonly StationInfoApp _stationInfoApp;

        public RouteInfoController(IMapper mapper, RouteInfoApp routeInfoApp, RouteDetailInfoApp routeDetailInfoApp, StationInfoApp stationInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._mapper = mapper;
            this._routeInfoApp = routeInfoApp;
            this._routeDetailInfoApp = routeDetailInfoApp;
            this._stationInfoApp = stationInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        public ActionResult RouteInfoAsync()
        {
            //await AddTracesInfo();
            return View();
        }

        /// <summary>
        /// 获取所有列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllRouteInfoAsync()
        {
            var where = new QueryRouteInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.FromStation = Request.Form["FromStation"].ToString();
            where.ToStation = Request.Form["ToStation"].ToString();

            var routeInfos = await _routeInfoApp.GetPagedRouteInfoAsync(where);
            var routeInfoPageDatas = new List<RouteInfoPageData>();
            if (routeInfos.Result != null && routeInfos.Result.Count > 0)
            {
                foreach (var item in routeInfos.Result)
                {
                    routeInfoPageDatas.Add(new RouteInfoPageData()
                    {
                        ID = item.ID,
                        FromStation = item.FromStation,
                        ToStation = item.ToStation
                    });
                }
            }
            var pageData = new TableData<RouteInfoPageData>(Draw, routeInfos.Total, routeInfos.Total, routeInfoPageDatas);

            //await AddTracesInfo(5, 0, "获取路径配置列表");
            return Json(pageData);
        }
        /// <summary>
        /// 添加获取编辑数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> RouteInfoAddOrEditAsync(long id)
        {
            var routeInfoPageData = new RouteInfoPageData();
            //提供工位选择  起始和目标工位
            var stationInfos = await _stationInfoApp.GetAllStationInfoAsync();
            routeInfoPageData.StationInfos = new List<StationInfoSearchData>();
            if (stationInfos != null && stationInfos.Count > 0)
            {
                foreach (var item in stationInfos)
                {
                    routeInfoPageData.StationInfos.Add(new StationInfoSearchData()
                    {
                        StationName = item.StationName,
                        StationCode = item.StationCode
                    });
                }
            }
            if (id > 0)
            {
                var routeInfo = await _routeInfoApp.GetRouteInfoByIdAsync(id);
                if (routeInfo != null)
                {
                    routeInfoPageData.ID = routeInfo.ID;
                    routeInfoPageData.FromStation = routeInfo.FromStation;
                    routeInfoPageData.ToStation = routeInfo.ToStation;
                }
            }
            return View(routeInfoPageData);
        }

        /// <summary>
        /// 添加或者编辑操作
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateRouteInfoAsync()
        {
            var routeInfo = new RouteInfo();
            routeInfo.ID = Request.Form["id"].ToInt64();
            routeInfo.FromStation = Request.Form["FromStation"].ToString();
            routeInfo.ToStation = Request.Form["ToStation"].ToString();

            bool result;
            string msg;
            try
            {
                if (routeInfo.ID == 0)
                {
                    result = await _routeInfoApp.CreateRouteInfoAsync(routeInfo);
                    await AddTracesInfo(2, 0, "新增路径配置");
                }
                else
                {
                    result = await _routeInfoApp.ModifyRouteInfoAsync(routeInfo);
                    await AddTracesInfo(2, 0, "修改路径配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }



        public async Task<JsonResult> DeleteRouteInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _routeInfoApp.DeleteRouteInfoByIdAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除路径配置");
            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 路径详情界面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> RouteDetailInfoByRouteIdAsync(long id)
        {
            var routeInfos = await _routeInfoApp.GetRouteInfoByIdAsync(id);
            return View(routeInfos);
        }

        /// <summary>
        /// 通过routeID获取路径详情
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetRouteDetailInfoByRouteIdAsync()
        {
            var routeId = Request.Form["routeId"].ToInt64();
            var routeDetailInfos = await _routeDetailInfoApp.GetRouteDetailInfoByRouteIDAsync(routeId);
            var routeDetailInfoDtos = _mapper.Map<List<RouteDetailInfo>, List<RouteDetailInfoDto>>(routeDetailInfos);

            var pageData = new TableData<RouteDetailInfoDto>(Draw, routeDetailInfoDtos.Count, routeDetailInfoDtos.Count, routeDetailInfoDtos);
            return Json(pageData);
        }
    }
}
